package com.xunyji.rabbitmq01.consumer;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.xunyji.rabbitmq01.entity.Order;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.util.Map;


/**
 * @author 王杨帅
 * @create 2018-08-30 20:51
 * @desc
 **/
@Component
public class OrderReceiver {

    @RabbitHandler // 开启监听
    @RabbitListener( // 指定监听那个队列
            bindings = @QueueBinding(
                    value = @Queue(value = "order-queue", durable = "true"),
                    exchange = @Exchange(value = "order-exchange", durable = "true", type = "topic"),
                    key = "order.#"

            )
    )
    public void onOrderMessage(
            @Payload Order order,
            @Headers Map<String, Object> headers,
            Channel channel
    ) throws Exception {

        System.out.println("=====收到消息，开始消费==========");
        System.out.println("获取到的订单ID为：" + order.getId());

        long deliverTag = (long) headers.get(AmqpHeaders.DELIVERY_TAG);
        channel.basicAck( deliverTag, false); // 手动签收，第二个参数：是否开启批量签收

    }

}

